package com.travel.server.manager.impl.command.article;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.article.DeleteArticleRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ArticleDal;
import com.travel.server.manager.model.ArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:13
 * @Desc:
 */
@Component
public class DeleteArticleCommand extends AbstractCommand<DeleteArticleRequest, EmptyResponse> {

    @Autowired
    private ArticleDal articleDal;

    @Override
    protected void checkInput(DeleteArticleRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteArticleRequest request) {
        checkInput(request);

        ArticleInfo articleInfo = articleDal.selectById(request.getId());
        articleInfo.setUpdateTime(new Date());
        articleInfo.setUpdateBy(getUserInfo().getName());
        articleInfo.setState(CommonConstant.NO);
        articleDal.insertOrUpdate(articleInfo);
        return new EmptyResponse();
    }
}
